package com.jl.commons.utils;

import com.jl.commons.constant.Constants;
import com.jl.model.*;
import com.jl.model.in.FaultInfoIn;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 简易的拷贝实体类
 *
 * @author JiangJian
 */
public class CopyBean {


    public static List<DeviceComponent> componentsToDeviceComponents(Long currentId, Device device, Long modelId,
                                                                     List<Component> componentList) {
        List<DeviceComponent> deviceComponentList = new ArrayList<>();
        Date now = new Date();
        if (null != componentList && !componentList.isEmpty()) {
            for (Component component : componentList) {
                DeviceComponent deviceComponent = new DeviceComponent();
                deviceComponent.setDeviceId(device.getId());
                deviceComponent.setDeviceNumber(device.getNumber());
                deviceComponent.setModelId(modelId);
                deviceComponent.setComponentId(component.getId());
                //配件的启用时间默认是设备的安装时间（暂定）待添加字段
                deviceComponent.setStartTime(device.getInstallDate());

                deviceComponent.setName(component.getName());
                deviceComponent.setUnit(component.getUnit());
                deviceComponent.setAvgLife(component.getAvgLife());
                deviceComponent.setLevel(component.getLevel());
                deviceComponent.setType(component.getType());
                deviceComponent.setDescription(component.getDescription());
                deviceComponent.setNumber(component.getNumber());
                deviceComponent.setCreateId(currentId);
                deviceComponent.setGmtCreate(now);
                deviceComponent.setStatus(1);
                deviceComponentList.add(deviceComponent);
            }
        }
        return deviceComponentList;
    }


    public static DamageComponent deviceComponentToDamageComponent(Long currUserId, FaultInfo faultInfo, DeviceComponent deviceComponent) {
        DamageComponent damageComponent = new DamageComponent();
        Date now = new Date();
        damageComponent.setName(deviceComponent.getName());
        damageComponent.setDescription(deviceComponent.getDescription());
        damageComponent.setNumber(deviceComponent.getNumber());
        damageComponent.setUnit(deviceComponent.getUnit());
        damageComponent.setType(deviceComponent.getType());
        damageComponent.setLevel(deviceComponent.getLevel());
        damageComponent.setDeviceId(deviceComponent.getDeviceId());
        damageComponent.setDeviceNumber(deviceComponent.getDeviceNumber());
//        damageComponent.setPartId(deviceComponent.getPartId());
        damageComponent.setComponentId(deviceComponent.getComponentId());
        damageComponent.setStartTime(deviceComponent.getStartTime());
        //应该以故障发生时间来作为其使用的结束时间
        damageComponent.setEndTime(faultInfo.getFaultTime());
        //目前都是默认只更换一个
        damageComponent.setChangeCount(1);
        //计算该配件的使用寿命，结束时间 - 开始时间 = 使用寿命
        Integer deviceComponentLife = TimeUtils.getIntervalDays(damageComponent.getStartTime(), damageComponent.getEndTime());
        damageComponent.setLife(deviceComponentLife);
        //状态码 暂时没有作用
        damageComponent.setStatus(1);
        damageComponent.setGmtCreate(now);
        damageComponent.setCreateId(faultInfo.getMasterSolver() == null ? currUserId : faultInfo.getMasterSolver());
        //更换了新的零件则表示需要重新设置零件的启用时间(故障解决时间为该配件的启用时间)
        deviceComponent.setStartTime(faultInfo.getSolveTime());
        return damageComponent;
    }

    public static DamageComponent deviceComponentToDamageComponent(Long currUserId, DeviceComponent deviceComponent) {
        DamageComponent damageComponent = new DamageComponent();
        Date now = new Date();

        damageComponent.setName(deviceComponent.getName());
        damageComponent.setDescription(deviceComponent.getDescription());
        damageComponent.setNumber(deviceComponent.getNumber());
        damageComponent.setUnit(deviceComponent.getUnit());
        damageComponent.setType(deviceComponent.getType());
        damageComponent.setLevel(deviceComponent.getLevel());
        damageComponent.setDeviceId(deviceComponent.getDeviceId());
        damageComponent.setDeviceNumber(deviceComponent.getDeviceNumber());
//        damageComponent.setPartId(deviceComponent.getPartId());
        damageComponent.setComponentId(deviceComponent.getComponentId());
        damageComponent.setStartTime(deviceComponent.getStartTime());
        //点击了配件更换则表示是在当前时间更换
        damageComponent.setEndTime(now);
        //目前都是默认只更换一个
        damageComponent.setChangeCount(1);
        //计算该配件的使用寿命，结束时间 - 开始时间 = 使用寿命
        Integer deviceComponentLife = TimeUtils.getIntervalDays(damageComponent.getStartTime(), damageComponent.getEndTime());
        damageComponent.setLife(deviceComponentLife);
        //状态码 暂时没有作用
        damageComponent.setStatus(1);
        damageComponent.setGmtCreate(now);
        damageComponent.setCreateId(currUserId);
        //重新设置该配件的启用时间，更换配件的时间(即当前时间)
        deviceComponent.setStartTime(now);
        return damageComponent;
    }

    public static FaultInfo faultInfoInToFaultInfo(FaultInfoIn faultInfoIn) {
        FaultInfo faultInfo = new FaultInfo();
        Date now = new Date();

        faultInfo.setDeviceId(faultInfoIn.getDeviceId());


        faultInfo.setSolver(faultInfoIn.getSolver());
        faultInfo.setSolveName(faultInfoIn.getSolveName());

        faultInfo.setFaultTime(faultInfoIn.getFaultTime());
        faultInfo.setSolveTime(faultInfoIn.getSolveTime());

        faultInfo.setResolvent(faultInfoIn.getResolvent());
        faultInfo.setKeyWordId(faultInfoIn.getKeyWordId());

        faultInfo.setStatus(faultInfoIn.getStatus());

        faultInfo.setSymptom(faultInfoIn.getSymptom());


        faultInfo.setGmtCreate(now);
        return faultInfo;

    }


    public static InspectionTask cycleTaskToInspectionTask(CycleTask cycleTask) {
        InspectionTask inspectionTask = new InspectionTask();
        Date now = new Date();

        //名称加上创建时间为后缀
        inspectionTask.setName(cycleTask.getName() + "-" + TimeUtils.parseDateToStr(now, "YYYY年MM月dd日"));
        inspectionTask.setDescription(cycleTask.getDescription());
        inspectionTask.setModelId(cycleTask.getModelId());
        inspectionTask.setContent(cycleTask.getContent());
        inspectionTask.setStartTime(now);
//        如果执行人不为空 则添加执行人信息
        if (null != cycleTask.getOperatorId()) {
            inspectionTask.setOperatorId(cycleTask.getOperatorId());
        }
        inspectionTask.setStatus(cycleTask.getStatus());
        inspectionTask.setIsTemp(Constants.NOT_TEMP_TASK);
        inspectionTask.setCreateId(cycleTask.getCreateId() == null ? cycleTask.getModifyId() : cycleTask.getCreateId());
        inspectionTask.setGmtCreate(now);
        return inspectionTask;

    }

}
